package com.modouyu.modules.crawler.spider.pipeline;

import com.alibaba.fastjson.JSON;
import com.modouyu.common.utils.RedisKeys;
import com.modouyu.common.utils.RedisUtils;
import com.modouyu.modules.crawler.entity.SpiderArticle;
import com.modouyu.modules.crawler.entity.SpiderChapter;
import com.modouyu.modules.crawler.spider.CrawlerLogProcessor;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * ${DESCRIPTION}
 *
 * @author yangcb
 * @email mudouyu@aliyun.com
 * @create 2018-01-10 16:36
 **/
public class CrawlerLogPipeline implements Pipeline {

    private String type;
    private RedisUtils redisUtils;
    /**
     * 队列章节key
     */
    public static final String CHAPTER_OBJ_KEY = "chapter:obj";
    /**
     * 队列目录key
     */
    public static final String ARTICLE_OBJ_KEY = "article:obj";

    public CrawlerLogPipeline() {
    }


    public CrawlerLogPipeline(RedisUtils redisUtils, String type) {
        this.redisUtils = redisUtils;
        this.type = type;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        if (resultItems.getAll().isEmpty()) {
            return;
        }
        Boolean ischapter = (Boolean) resultItems.getAll().get("ischapter");
        if (null == ischapter) return;
        if (ischapter) {
            SpiderChapter spiderChapter = JSON.parseObject(JSON.toJSONString(resultItems.getAll()), SpiderChapter.class);
            if (spiderChapter != null) {
                redisUtils.lPush(RedisKeys.getCrawlerLogKey(CHAPTER_OBJ_KEY), spiderChapter);
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.CHAPTER_URL_KEY, type)), spiderChapter.getChapterurl());
            }
        } else {
            SpiderArticle spiderArticle = JSON.parseObject(JSON.toJSONString(resultItems.getAll()), SpiderArticle.class);
            if (spiderArticle != null) {
                spiderArticle.setTitle(spiderArticle.getTitle().replace("全文阅读", ""));
                spiderArticle.setType(type);
                redisUtils.lPush(RedisKeys.getCrawlerLogKey(ARTICLE_OBJ_KEY), spiderArticle);
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.ARTICLE_AUTHOR_KEY, type)), spiderArticle.getArticleurl());
            }
        }
    }


}
